Page({
    data: {
      username: 'lk',
      password:'123456',
      code: '',
      codeUrl:'',
      uuid:'',
      captchaEnabled:true
    },
    onLoad: function () {
        this.getCode()
    },
    onUsernameInput: function (e) {
      this.setData({
        username: e.detail.value
      });
    },
    onPasswordInput: function (e) {
      this.setData({
        password: e.detail.value
      });
    },
    onCodeInput: function (e) {
      this.setData({
        code: e.detail.value
      });
    },
    onLogin: function () {
      const loginForm = {
        username: this.data.username,
        password: this.data.password,
        code: this.data.code,
        uuid:this.data.uuid
      };
      if(loginForm.username==='' && loginForm.password===''){
        wx.showToast({
          title: '请输入完整信息!',
          icon:'error'
        })
        return 
      }
      // 这里假设后端的登录接口地址为https://yourdomain.com/login
      wx.request({
        url: 'http://10.112.10.129:8080/login',
        method: 'POST',
        data: loginForm,
        success: function (res) {
            if(res.data.code!==200){
                wx.showToast({
                  title: res.data.msg,
                  icon:'error'
                })
            }else{
                wx.showToast({
                  title: '登录成功',
                  icon:'success'
                })
                // 存放token
                wx.setStorageSync('hasToken', res.data.token);
                setTimeout(() => {
                    wx.navigateTo({
                      url: '/pages/index/index',
                    })
                }, 500);
            }
        //   console.log('登录成功', res);
          // 可以在这里进行登录成功后的页面跳转或其他操作
        },
        fail: function (err) {
          console.log('登录失败', err);
          // 可以在这里处理登录失败的提示信息等
        }
      });
    },
    /**
     * 获取登录验证码
     */
    getCode(){
        wx.request({
          url: 'http://10.112.10.129:8080/captchaImage',
          method: 'GET',
          timeout:20000,
          success: (res)=> {
            // console.log('验证码获取', res.data.captchaEnabled)
            this.setData({
                captchaEnabled : res.data.captchaEnabled === undefined ? true : res.data.captchaEnabled
            })
            if (this.data.captchaEnabled) {
                this.setData({
                    codeUrl : "data:image/gif;base64," + res.data.img,
                    uuid :res.data.uuid
                })
            }
    
        },
        fail: function (err) {
          console.log('验证码获取失败', err);
          // 可以在这里处理登录失败的提示信息等
        }
        })
        
    }
  });